package no.rodland.acc.db;

import java.util.Comparator;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Aug 2, 2010 11:43:56 AM
 * @Author: fmr
 */
public class DbRowComparator implements Comparator<DbRow> {
    private boolean ascending = false;

    DbRowComparator() {
    }

    DbRowComparator(boolean ascending) {
        this.ascending = ascending;
    }

    @Override
    public int compare(DbRow o1, DbRow o2) {
        if (o1 == o2) {
            return 0;
        }

        if (o1 == null) {
            return 1;
        }
        else if (o2 == null) {
            return -1;
        }
        int comparison;
        if (ascending) {
            comparison = o1.getDate().compareTo(o2.getDate());
        }
        else {
            comparison = o2.getDate().compareTo(o1.getDate());
        }

        if (comparison != 0) {
            return comparison;
        }

        return o1.getText().compareTo(o2.getText());
    }
}
